package com.example.smartcity_poverty.ui.AllService.RubbishClassify

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcBannerModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity.RcClassifyActivity
import com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity.RcSeekActivity
import com.example.smartcity_poverty.ui.AllService.RubbishClassify.fragment.RcNewsFragment
import com.example.smartcity_poverty.ui.AllService.RubbishClassify.fragment.RcNewsFragment.Companion.RcIdNumber
import com.google.android.material.tabs.TabLayout
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_rubbish_classify.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/**
 * 垃圾分类开发
 */
class RubbishClassifyActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_rubbish_classify)
        title = "垃圾分类"

        // 主页Banner
        setBanner()

        // 选项卡
        setTabLayoutToViewPager()

        // 点击进入搜索页
        rcHomeSeek.setOnClickListener {
            startActivity(Intent(this, RcSeekActivity::class.java))
        }

        // 点击进入分类
        rcHomeClassify.setOnClickListener {
            startActivity(Intent(this, RcClassifyActivity::class.java))
        }

    }

    /**
     * 选项卡设置
     */
    private fun setTabLayoutToViewPager() {
        val strTitle = listOf<String>("新时尚","党员在行动","分类达人","社区动态")
        // 添加到TabLayout
        for (i in strTitle){
            rcTabLayout.addTab(rcTabLayout.newTab().setText(i))
        }

        // ViewPager2适配器
        rcViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int = 4

            override fun createFragment(position: Int): Fragment
                = when(position){
                0 -> RcNewsFragment()
                1 -> RcNewsFragment()
                2 -> RcNewsFragment()
                else -> RcNewsFragment()
            }

            override fun getItemId(position: Int): Long {
                RcIdNumber = when(position){
                    0 -> 7
                    1 -> 8
                    2 -> 9
                    else -> 10
                }
                return super.getItemId(position)
            }

        }

        // 监听TabLayout
        rcTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                rcViewPager2.currentItem = tab?.position!!
            }

        })


        // 监听ViewPager2
        rcViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                rcTabLayout.getTabAt(position)?.select()
            }
        })

    }

    /**
     * 展示垃圾分类新闻宣传轮播图
     */
    private fun setBanner() {
        // 存储照片
        val array = ArrayList<String>()

        // 判断token是否为空
        if (getToken().isNotEmpty()) {
            // 开启协程发起网络请求
            CoroutineScope(Dispatchers.IO).launch {
                // 获取网络轮播图
                api.getRcBanner(getToken()).apply {
                    // 判断是否访问成功！
                    if (this.code == 200) {
                        val data = this.data
                        runOnUiThread {
                            // banner适配器
                            rcBanner.apply {
                                this.adapter = object :
                                    BannerImageAdapter<RcBannerModel.DataBean>(data) {
                                    override fun onBindView(
                                        p0: BannerImageHolder?,
                                        p1: RcBannerModel.DataBean?,
                                        p2: Int,
                                        p3: Int
                                    ) {
                                        // 网络加载图片
                                        glide(url + p1?.imgUrl.toString(), p0?.imageView!!)
                                    }
                                }
                                this.addBannerLifecycleObserver(this@RubbishClassifyActivity).indicator = CircleIndicator(context)
                                this.setBannerRound(25f)
                            }
                        }
                    } else runOnUiThread {
                        // 弹窗错误！
                        this.msg?.show()
                    }
                }
            }
        } else "请先登录！".show()
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}